package com.java2207.group1.controller;

import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.common.R;
import com.java2207.group1.pojo.Room;
import com.java2207.group1.service.RoomService;
import com.java2207.group1.util.OssUtil;
import com.java2207.group1.vo.consult.BarVo;
import com.java2207.group1.vo.consult.PieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @author :xs
 * @date : 2022/7/26
 */
@RestController
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;
    private Map<String, Object> params;

    //分页
    @RequestMapping("/listRoom")
    public LayuiPageVo<Room> roomPageData(@RequestParam Map<String, Object>params){
        return roomService.roomPage(params);
    }

    //分页
    @RequestMapping("/deleteRoom")
    public R deleteRoom(Integer id){
        int i = roomService.deleteRoom(id);
        if(i==1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //删除
    @GetMapping("/remove")
    public R deleteRoomsByIds(String ids){
        int result = roomService.remove(ids);
        if (result>0){
            return  R.ok();
        }else {
            return  R.error();
        }
    }

    //添加
    @RequestMapping("/addRoom")
    public R addRoom(Room room){
        int i = roomService.addRoom(room);
        if (i==1){
            return  R.ok();
        }else {
            return R.error();
        }
    }


    @RequestMapping("/updateRoom")
    public R updateRoom(Room room){
        int i = roomService.updataRoom(room);
        if(i==1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @RequestMapping("/echaetsPie")
    public R echaetsPie(){
        List<PieVo>list = roomService.TypeNameData();
        return R.ok().data("typePie",list);
    }

    @RequestMapping("/echrtBar")
    public R echartBar(){
        List<BarVo>list = roomService.TypeNameBarData();
        return R.ok().data("typeBar",list);
    }

    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {

        if(file!=null){
            //得到文件名
            String originalFilename = file.getOriginalFilename();

            InputStream inputStream = file.getInputStream();

            String url = OssUtil.uploadFile((FileInputStream) inputStream, originalFilename);

            //把文件 url 写给前端

            return R.ok().data("url",url);
        }
        return R.ok();
    }


}
